// 登录后的程序主页

// 移除VSCode非必要报警
// ignore_for_file: unnecessary_brace_in_string_interps, non_constant_identifier_names, use_key_in_widget_constructors, must_be_immutable, unused_element, no_leading_underscores_for_local_identifiers, camel_case_types, unused_local_variable

import 'package:flutter/material.dart';
import 'package:project_award_statistics/pages/mobile_material_design/login_page/login_page.dart';
import 'package:project_award_statistics/utils/load_award_info_from_db/award_model.dart';
import 'package:project_award_statistics/utils/load_award_info_from_db/load_award_info.dart';
import 'package:project_award_statistics/utils/mobile_material_design/my_drawer/my_drawer.dart';

class HomePage_Mobile extends StatefulWidget {
  // 从登录界面中获得学生姓名(用户名)
  String stu_name;
  
  @override
  HomePage_Mobile({  //初始化函数信息
    super.key,
    required this.stu_name,
  });

  @override
  State<HomePage_Mobile> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage_Mobile> with TickerProviderStateMixin {
  List<award_model> user_award_list = List.empty(); // 初始化用户获奖信息列表对象
  
  // 初始化动画总线控制器1
  late AnimationController _animationController_widget1;
  // 初始化动画总线控制器2
  late AnimationController _animationController_widget2;
  // 初始化动画总线控制器3
  late AnimationController _animationController_widget3;
  // 初始化动画总线控制器4
  late AnimationController _animationController_widget4;
  @override
  void initState()   
    {
      super.initState();  // 重写该StatefulWidget的初始化方法

      _animationController_widget1=AnimationController(  
        duration: Duration(seconds:2),  // 设置动画持续时间
        vsync: this,
      );
      _animationController_widget2=AnimationController(  
        duration: Duration(seconds:2),  // 设置动画持续时间
        vsync: this,
      );
      _animationController_widget3=AnimationController(  
        duration: Duration(seconds:2),  // 设置动画持续时间
        vsync: this,
      );
      _animationController_widget4=AnimationController(  
        duration: Duration(seconds:3),  // 设置动画持续时间
        vsync: this,
      );

      //控件动画,启动!
      _animationController_widget1.forward().then((_){
        //控件1动画完成后显示控件2动画
        Future.delayed(Duration(seconds:0)).then((_){
          _animationController_widget2.forward().then((_){
            //控件2完成后再显示控件3动画
            Future.delayed(Duration(seconds:0)).then((_){
              _animationController_widget3.forward().then((_){
                //控件3完成后再显示控件4动画
                Future.delayed(Duration(seconds:0)).then((_){
                  _animationController_widget4.forward();
                });
              });
            });
          });  //控件2动画,启动!
        });
      });
      update_user_award_list(); //更新user_award_list (用户获奖信息)函数
    }
  Future <void> update_user_award_list() async // 该函数用于获取用户的获奖信息
    {
      List<award_model> updated_user_award_list = await DBProvider_AwardInfo().get_award_list(widget.stu_name);
      setState(() {
        //更新statefulWidget里的user_award_list变量
        user_award_list = updated_user_award_list;
      });
    }

  @override
  Widget build(BuildContext context) {
    // 获取传入的用户名
    final stu_name = widget.stu_name; 
      
    void animation_dispose ()   // 如果动画未播放完就退出应用,那么先停止动画再释放动画总线对象
      {
        _animationController_widget1.stop();
        _animationController_widget2.stop();
        _animationController_widget3.stop();
        _animationController_widget4.stop();
        _animationController_widget1.dispose();
        _animationController_widget2.dispose();
        _animationController_widget3.dispose();
        _animationController_widget4.dispose();
      }

    @override
    void dispose ()   // 覆写析构函数
      {
        animation_dispose();
        super.dispose();
      }   

    // 初始化页面UI框架
    return Scaffold(
      
      // 设置应用顶栏内容
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
        foregroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text(
          // 设置标题栏文字
          "个人资料",
        ),
        actions: [
          // 设置右侧退出到主页的按钮
          IconButton(
            onPressed:
              () {
                animation_dispose();
                Navigator.pushReplacement(  // 设置按钮跳转功能
                  context, 
                  MaterialPageRoute(
                    builder: (context)=>PopScope(                   
                      canPop: false,
                      child: LoginPage_Mobile(), 
                    )
                  ),
                );   
              },
            icon: const Icon(Icons.exit_to_app),   // 设置图标
          ),
        ],
      ),

      // 设置侧边栏内容
      drawer: MyDrawer(current_page: "/home_page", stu_name: stu_name),

      // 设计本体内容
      body: Padding(
        padding: EdgeInsets.only(left: 25,top: 10,right: 25),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
          // 下面显示个人获奖信息
            //先显示欢迎动画及字样

            //先显示第一行"欢迎, ${USER}
            SlideTransition(  
              position:Tween<Offset>(
                begin: Offset(-1.5,0),
                end: Offset.zero,
              ).animate(
                CurvedAnimation(
                  parent:_animationController_widget1,
                  curve:Curves.easeInOut,
                ),
              ),
              child: Text(
                "欢迎, ${stu_name}",
                style: TextStyle(
                  color: Colors.black87,
                  fontSize: 50,
                ),
              ),
            ),

            //再显示第二行"光阴似箭,.."一堆鸡汤
            const SizedBox(height: 35,),

            SlideTransition(
              position:Tween<Offset>(
                begin: Offset(-1.5,0),
                end: Offset.zero,
              ).animate(
                CurvedAnimation(
                  parent:_animationController_widget2,
                  curve:Curves.easeInOut,
                ),
              ),
              child: Text(
                "光阴似箭,日月如梭",
                style: TextStyle(
                  color: Colors.black87,
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            const SizedBox(height: 20,),  //设置控件间距
            //再显示动画及个人获奖信息
            SlideTransition(
              position:Tween<Offset>(
                begin: Offset(-1.5,0),
                end: Offset.zero,
              ).animate(
                CurvedAnimation(
                  parent:_animationController_widget3,
                  curve:Curves.easeInOut,
                ),
              ),
              child: user_award_list.isEmpty   //判断用户有无获奖,并输出不同文字
              ? Text(
                "在大学生活中,你目前还没有获得奖项",
                style: TextStyle(
                  color: Colors.black87,
                  fontSize: 24,
                ),
              )
              : Text(
                "在大学生活中,你获得了${user_award_list.length}个奖项",
                style: TextStyle(
                  color: Colors.black87,
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            const SizedBox(height: 35,),   // 设置控件间距
            SlideTransition(
              position:Tween<Offset>(
                begin: Offset(-1.5,0),
                end: Offset.zero,
              ).animate(
                CurvedAnimation(
                  parent:_animationController_widget4,
                  curve:Curves.easeInOut,
                ),
              ),
              child: Text(
                "希望你在接下来的学习生涯中坚持不懈,久久为功,赢得更多的奖项!",
                style: TextStyle(
                  color: Colors.black87,
                  fontSize: 26,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
